import type { ResponseData, ResponsePaginationData } from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import { message } from 'antd';
import { request } from 'umi';
import type { QueryParams, Zone, ZoneTableListItem } from './types';

//查询单个数据
export async function getParkDetail(id: string) {
  return await request(`/zone/${id}`);
}

//停车场列表
export async function getParkTable(
  params: QueryParams,
): Promise<ResponsePaginationData<RequestData<Zone>>> {
  console.log('params', params);
  const addressCode = params.addressCode && params.addressCode[params.addressCode.length - 1];
  return await request('/zone/page', {
    params: {
      ...params,
      addressCode,
      creatorId: params.creatorName && params.creatorName?.id,
      page: params.current,
      pageSize: params.pageSize,
      tenant_id: params.tenant && params.tenant?.id,
    },
  });
}

//删除停车场
export async function deletePark(id: string): Promise<ResponsePaginationData<RequestData<Zone>>> {
  return await request(`/zone/${id}`, {
    method: 'DELETE',
  });
}

//创建一条停车场信息
export async function addParkItem(
  tenantId: string,
): Promise<ResponsePaginationData<RequestData<Zone>>> {
  return await request('/zone/create', {
    method: 'POST',
    data: {
      tenantId: tenantId,
    },
  });
}

//申请创保存停车场的信息
export async function saveParkItem(
  params: any,
): Promise<ResponsePaginationData<RequestData<Zone>>> {
  return await request('/zone/save', {
    method: 'POST',
    data: {
      // schemeId:params?.scheme.id;
      ...params,
    },
  });
}

//申请创建停车场的信息
export async function applyParkItem(
  params: any,
): Promise<ResponsePaginationData<RequestData<Zone>>> {
  return await request('/zone/apply', {
    method: 'POST',
    data: {
      ...params,
    },
  });
}

/**
 * 获取停车区域列表
 * @param params
 */
export async function getZoneSelectionList(
  params: Pagination & { name?: string; no?: string; tenant_id?: string; address_code?: string },
): Promise<RequestData<ZoneTableListItem>> {
  const res: ResponsePaginationData<{
    id: string;
    name?: string;
  }> = await request('/zone/get_zone_options', {
    params: {
      ...params,

      page: params.page || 1,
      pageSize: params.pageSize || 10,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }

  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

//获取相机分页查询
export async function gateCameraItem(params: {
  text?: string;
  tenantId: number;
  page: number;
  pageSize: number;
}): Promise<ResponseData<Zone>> {
  return await request('/gate/camera', {
    params: {
      ...params,
    },
  });
}

//获取用户选项列表
export async function gateAdminItem(params: {
  userName?: string;
  name: string;
  page?: number;
  pageSize?: number;
}): Promise<ResponseData<any>> {
  const res = await request('/sys/admin/get_admin_options', {
    params: {
      page: 1,
      pageSize: 10,
      ...params,
    },
  });
  if (res) return { code: res.code, data: res.data.list, success: true, message: '' };
  return res;
}
